package com.example.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 获取远程数据工具包
 * 
 * @author Administrator
 *
 */
public class HttpClientUtil {
	// 解析淘宝口令地址
	private static final String JXURL = "https://api.open.21ds.cn/apiv1/tpwdtoid?apkey=fe73f41c-1789-e6fa-2e8a-51eae28a0da9&tpwd=";
	// 获取优惠券
	private static final String yhqURL = "https://api.open.21ds.cn/apiv1/getitemgyurl?apkey=fe73f41c-1789-e6fa-2e8a-51eae28a0da9&pid=mm_124952399_34694920_122402473&shorturl=1&tpwd=1&tbname=韩导业务&itemid=";
	// 获取简易商品信息
	private static final String details = "https://api.open.21ds.cn/apiv1/getiteminfo?apkey=fe73f41c-1789-e6fa-2e8a-51eae28a0da9&itemid=";
	/**
	 * 
	 * 获取token接口
	 * 
	 */

	private String getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxa23c0fb290b3e172&secret=a4b8d1f7b197a73987ffc051642e9cab";

	/**
	 * 
	 * 拉微信用户信息接口
	 * 
	 */

	private String getUserInfoUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}";

	/**
	 * 
	 * 主动推送信息接口
	 * 
	 */

	private String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/send?access_token={0}";

	public static String getHttpInterface(String httpUrl) {
		BufferedReader in = null;
		StringBuffer result = null;
		try {
			URL url = new URL(httpUrl);
			// 打开和url之间的连接
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			connection.setRequestProperty("Charset", "utf-8");
			connection.connect();

			result = new StringBuffer();
			// 读取URL的响应
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result.append(line);
			}
			return result.toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 解析淘口令
	 * 
	 * @param args
	 */
	public static String jxTb(String message) {
		if (message.indexOf("￥") == -1) {
			return null;
		}
		String tkl = message.substring(message.indexOf("￥"), message.indexOf("￥") + 13);
		return tkl;
	}

	/**
	 * json转list 获取多个数据
	 * 
	 * @param args
	 */
	public static List<Map<String, String>> jsonToMap(String json, String data, List<String> dataList) {
		JSONObject obj = JSON.parseObject(json);
		JSONArray jsonArray = obj.getJSONArray(data);
		List<Map<String, String>> datalist = new ArrayList<>();
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject item = jsonArray.getJSONObject(i);
			Map<String, String> dataMap = new HashMap<>();
			for (String str : dataList) {
				dataMap.put(str, item.getString(str));
			}
			datalist.add(dataMap);
		}
		return datalist;
	}

	/**
	 * 通过淘宝口令获取商品id
	 * 
	 * @param kl
	 * @return
	 */
	public static String acquireId(String kl) {
		String h = getHttpInterface(JXURL + kl);
		return h;
	}

	/**
	 * 获取优惠券信息 单品转链接
	 * 
	 * @param kl
	 * @return
	 */
	public static String yhq(String kl) {
		String h = getHttpInterface(yhqURL + kl);
		return h;
	}

	/**
	 * 获取商品简易信息
	 * 
	 * @param kl
	 * @return
	 */
	public static String detail(String kl) {
		String h = getHttpInterface(details + kl);
		return h;
	}

	public static void main(String[] args) {
		String h = getHttpInterface("http://api.vephp.com/products?vekey=V00002158Y65810607");
		JSONObject obj = JSON.parseObject(h);
		JSONArray jsonArray = obj.getJSONArray("data");
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject item = jsonArray.getJSONObject(i);

			System.out.print("title: " + item.getString("title"));
			System.out.println();
		}
		String test = "11";
		System.out.println(test.indexOf("￥"));
		System.out.println(test.substring(test.indexOf("￥"), test.indexOf("￥") + 13));
	}
}
